package com.chcit.mobile.mvp.inventory.presenter;

import android.app.Application;

import com.alibaba.fastjson.JSONObject;
import com.chcit.custom.view.util.DialogUtil;
import com.chcit.mobile.app.utils.AppUtils;
import com.chcit.mobile.common.SPDErrorHandle;
import com.chcit.mobile.mvp.common.contract.CommonContract;
import com.chcit.mobile.mvp.entity.BaseData;
import com.chcit.mobile.mvp.entity.ResultBean;
import com.chcit.mobile.mvp.inventory.adapter.InventoryPackagePlanAdapter;
import com.chcit.mobile.mvp.inventory.contract.InventoryPlanContract;
import com.chcit.mobile.mvp.inventory.entity.InventoryPackageBean;
import com.jess.arms.base.app.Quick;
import com.jess.arms.di.scope.FragmentScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.RxLifecycleUtils;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;

@FragmentScope
public class InventoryPlanPackagePresenter extends BasePresenter<InventoryPlanContract.Model,InventoryPlanContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;
    @Inject
    InventoryPackagePlanAdapter mAdapter;
    @Inject
    CommonContract.Model commonModel;
    private boolean isLoading;
    private int mStart;
    private int mCount = 30;
    @Inject
    public InventoryPlanPackagePresenter(InventoryPlanContract.Model model, InventoryPlanContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public void getData(JSONObject json, Consumer<List<InventoryPackageBean>> onNext){
        if (!isLoading) {
            isLoading = true;
            json.put("start", mStart);
            json.put("limit", mCount);
            mModel.getData(json)
                    .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                    .observeOn(AndroidSchedulers.mainThread())
                    .doOnSubscribe(disposable -> {
                        mRootView.showLoading();
                    })
                    .subscribe(new SPDErrorHandle<BaseData<InventoryPackageBean>>(mErrorHandler){

                        @Override
                        public void onNext(BaseData<InventoryPackageBean> response) {
                            isLoading = false;
                            try {

                               if(response.getTotal() == 0){
                                   mRootView.showMessage("没有查到盘点数据！");
                                   mAdapter.notifyDataSetChanged();
                               }else if(response.getTotal()>mStart){
                                    mStart += mCount;
                                    onNext.accept(response.getRows());
                                    if(mStart>=response.total){
                                        mRootView.showNoMoreData();
                                    }
                                }else{
                                   mRootView.showNoMoreData();
                               }

                            } catch (Exception e) {
                                onError(e);
                            }
                        }

                        @Override
                        public void onError(Throwable t) {
                            isLoading = false;
                            mRootView.hideLoading();
                            super.onError(t);
                        }

                        @Override
                        public void onComplete() {
                            mRootView.hideLoading();
                        }
                    });
        }

    }
    public void doChecked(JSONObject json, Action action){
        commonModel.doChecked(json)
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(disposable -> {
                    mRootView.showLoading();
                })
                .doFinally(() -> {
                    mRootView.hideLoading();
                }).subscribe(new SPDErrorHandle<ResultBean>(mErrorHandler) {

            @Override
            public void onNext(ResultBean resultBean) {

                try {
                    if(resultBean.isSuccess()){
                        action.run();
                    }else{
                        DialogUtil.showErrorDialog(mRootView.getActivity(),resultBean.getMsg());
                    }

                } catch (Exception e) {
                    onError(e);
                }
            }
        });
    }
    public void setStart(int mStart) {
        this.mStart = mStart;
    }
}
